package com.mcxx.modules.extremepoverty.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.extremepoverty.service.ExCaregiverService;
import com.mcxx.modules.miniSalvation.family.dto.SalvationObjectDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderCaregiversDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderSuspendCareDTO;
import com.mcxx.modules.miniSalvation.order.param.CaregiverParam;
import com.mcxx.modules.miniSalvation.order.service.read.OrderCareLinkReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderCaregiversReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderSuspendCareReadService;
import com.mcxx.modules.miniSalvation.order.service.write.OrderCaregiversWriteService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.mcxx.validate.group.Creation;
import com.mcxx.validate.group.Update;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Slf4j
@Validated
@Api("照料人管理控制类")
@RestService({"extremePoverty/caregiver/v2"})
public class ExCaregiverController extends DefaultRestService {
    @Autowired
    private OrderCaregiversReadService orderCaregiversReadService;
    @Autowired
    private OrderCaregiversWriteService orderCaregiversWriteService;
    @Autowired
    private OrderCareLinkReadService orderCareLinkReadService;
    @Autowired
    private ExCaregiverService exCaregiverService;
    @Autowired
    private OrderSuspendCareReadService orderSuspendCareReadService;


    @PostMapping("getSalvationObjectList")
    @ApiOperation("查询供养对象列表")
    public void getSalvationObjectList(CaregiverParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        log.info("查询供养对象列表,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderCaregiversReadService.getSalvationObjectList(param,pageParam,getCurUserAccount());
        setPageBean(page);
        log.info("查询供养对象列表完成，结果：{}",JSON.toJSONString(page));
    }


    @PostMapping("listSalvationObject4Alteration")
    @ApiOperation("变更照料人列表")
    public void listSalvationObject4Alteration(CaregiverParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        log.info("查询供养对象列表,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderCaregiversReadService.listSalvationObject4Alteration(param,pageParam,getCurUserAccount());
        setPageBean(page);
        log.info("查询供养对象列表完成，结果：{}",JSON.toJSONString(page));
    }


    @PostMapping("addCaregiver")
    @ApiOperation("登记照料人")
    public void addCaregiver(@Validated(Creation.class) OrderCaregiversDTO dto){
        log.info("登记照料人,参数：{}", JSON.toJSONString(dto));
        orderCaregiversWriteService.addCaregiver(dto,getCurUserAccount());
        log.info("登记照料人完成");
    }


    @PostMapping("listCaregiver")
    @ApiOperation("获取照料人列表")
    public void listCaregiver(CaregiverParam param, PageParam pageParam){
        log.info("获取照料人列表,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderCareLinkReadService.listCaregiver(param,pageParam,getCurUserAccount());
        setPageBean(page);
        log.info("获取照料人列表完成，结果：{}",page);
    }



    @PostMapping("updateCaregiver")
    @ApiOperation("变更照料人")
    public void updateCaregiver(@Validated(Update.class) OrderCaregiversDTO dto){
        log.info("变更照料人,参数：{}", JSON.toJSONString(dto));
        orderCaregiversWriteService.updateCaregiver(dto,getCurUserAccount());
        log.info("变更照料人完成");
    }


    @GetMapping("getCaregiverById")
    @ApiOperation("变更照料人数据回显")
    public void getCaregiverById(@V({"notnull"}) String id,@V({"notnull"}) String memberId){
        log.info("变更照料人数据回显，参数：{}", id);
        OrderCaregiversDTO data = orderCaregiversReadService.getCaregiverById(id,memberId);
        setData(data);
        log.info("变更照料人数据回显完成，结果：{}", data);
    }


    @PostMapping("deleteCaregiver")
    @ApiOperation("注销照料人信息")
    public void deleteCaregiver(@V({"notnull"}) String id,@V({"notnull"}) String memberId,@V({"notnull"}) String standType,@V({"notnull"}) String caregiverType){
        log.info("注销照料人信息，参数：{}", id);
        orderCaregiversWriteService.deleteCaregiver(id,memberId,standType,caregiverType,getCurUserAccount());
        log.info("注销照料人信息完成");
    }

    @PostMapping("downLoadCareList")
    @ApiOperation("照料人导出")
    public void downLoadCareList(HttpServletResponse response, @RequestBody CaregiverParam param)throws IOException {
        log.info("照料人导出,查询参数：{},分页参数：{}", JSON.toJSONString(param));
        ExcelInfo excelInfo = orderCaregiversWriteService.exportCareList(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }



    @GetMapping("getCaregiverCountSummary")
    @ApiOperation("根据照料人身份证号查询 总照料金额")
    public void getCaregiverCountSummary(@V({"notnull"}) CaregiverParam param) {
        log.info("根据照料人身份证号查询 总照料金额，参数：{}", param);
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        double familyRevenue = orderCaregiversReadService.getCareGiverCountSummary(param,getCurUserAccount());
        setData(familyRevenue);
        log.info("根据照料人身份证号查询 总照料金额完成，参数：{}", param);
    }


    @GetMapping("getCaregiverInfo")
    @ApiOperation("根据照料人身份证号查询 被照料人信息")
    public void getCaregiverInfo(@V({"notnull"}) CaregiverParam param) {
        log.info("根据照料人身份证号查询 被照料人信息，参数：{}", param);
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        List<SalvationObjectDTO> SalvationObjectLists = orderCaregiversReadService.getCareGiverInfo (param,getCurUserAccount());
        setData(SalvationObjectLists);
        log.info("根据照料人身份证号查询 被照料人信息，参数：{}", param);
    }


    @GetMapping("getCaregiverByIdcard")
    @ApiOperation("根据身份证号查询照料人数据回显")
    public void getCaregiverByIdcard(@V({"notnull"}) String idcard){
        log.info("根据身份证号查询照料人数据回显，参数：{}", idcard);
        OrderCaregiversDTO data = orderCaregiversReadService.getCaregiverByIdcard(idcard);
        setData(data);
        log.info("根据身份证号查询照料人数据回显完成，结果：{}", data);
    }


    @PostMapping("getSuspendObjectList")
    @ApiOperation("查询暂停护理费供养对象列表")
    public void getSuspendObjectList(CaregiverParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        log.info("查询暂停护理费供养对象列表,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderCaregiversReadService.getSuspendObjectList(param,pageParam,getCurUserAccount());
        setPageBean(page);
        log.info("查询暂停护理费供养对象列表完成，结果：{}",JSON.toJSONString(page));
    }


    @PostMapping("findFundPayDate")
    @ApiOperation(value="校验名册", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="校验名册")
    public void findFundPayDate(String orderId){
        log.info("开始校验名册，参数是：{}", orderId);
        Map<String,String> res = exCaregiverService.findFundPayDate(orderId);
        setData(res);

        log.info("开始校验名册，查询结果是：{}", res);
    }


    @RequestMapping(value = "suspendCare")
    @ApiOperation(value = "护理费暂停列表操作按钮")
    public void suspendCare(@V({"notnull"}) OrderSuspendCareDTO dto) {
        log.info("护理费暂停列表操作按钮,条件是{}", dto);
        dto.setBusinessType(Constant.BusinessCode.TEKUN);
        //param.setStopMonth(param.getStopMonth().replaceAll("-", ""));
//        param.setStopMonth(param.getStopMonth());
        BaseResult<Boolean> baseResult = exCaregiverService.suspendCare(dto, getCurUserAccount());
        setData(baseResult);
        log.info("护理费暂停列表暂停操作按钮,结果是{}", baseResult);
    }


    @PostMapping("getSuspendList")
    @ApiOperation("暂停对象查询列表")
    public void getSuspendList(CaregiverParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        log.info("暂停对象查询列表开始,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderSuspendCareReadService.getSuspendList(param,pageParam,getCurUserAccount());
        setPageBean(page);
        log.info("暂停对象查询列表结束，结果：{}",JSON.toJSONString(page));
    }



    @RequestMapping(value = "recoveryCare")
    @ApiOperation(value = "护理费暂停列表操作按钮")
    public void recoveryCare(@V({"notnull"}) OrderSuspendCareDTO dto) {
        log.info("护理费暂停列表操作按钮,条件是{}", dto);
        dto.setBusinessType(Constant.BusinessCode.TEKUN);
        //param.setStopMonth(param.getStopMonth().replaceAll("-", ""));
//        param.setStopMonth(param.getStopMonth());
        BaseResult<Boolean> baseResult = exCaregiverService.recoveryCare(dto, getCurUserAccount());
        setData(baseResult);
        log.info("护理费暂停列表暂停操作按钮,结果是{}", baseResult);
    }


    @GetMapping("getObjectCountSummary")
    @ApiOperation("根据条件统计暂停对象总人数、总金额")
    public void getObjectCountSummary(CaregiverParam param) {
        log.info("根据条件统计暂停对象总人数、总金额，参数：{}", JSON.toJSONString(param));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.TEKUN);
        Map data = orderSuspendCareReadService.getObjectCountSummary(param,accountVo);
        setData(data);
        log.info("根据条件统计暂停对象总人数、总金额完成，结果：{}", JSON.toJSONString(data));
    }
}
